<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>php 使用 redis 储存 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>php 使用 redis 储存</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<h2 id="1-redis-扩展模块安装"><a href="#1-redis-扩展模块安装" class="headerlink" title="1.redis 扩展模块安装"></a>1.redis 扩展模块安装</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载模块</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@redis ~]<span class="comment"># wget https://codeload.github.com/phpredis/phpredis/zip/develop</span></div><div class="line">[root@redis ~]<span class="comment"># mv develop phpredis.zip</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以下载的时候就重命名</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@redis ~]<span class="comment"># wget https://codeload.github.com/phpredis/phpredis/zip/develop -O phpredis.zip</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@redis ~]<span class="comment"># unzip phpredis.zip </span></div><div class="line">[root@redis ~]<span class="comment"># cd phpredis-develop/</span></div><div class="line">[root@redis phpredis-develop]<span class="comment"># /usr/local/php/bin/phpize </span></div><div class="line">[root@redis phpredis-develop]<span class="comment"># ./configure --with-php-config=/usr/local/php/bin/php-config </span></div><div class="line">[root@redis phpredis-develop]<span class="comment"># make &amp;&amp; make install</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑 /usr/local/php/etc/php.ini </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@redis ~]<span class="comment"># vim /usr/local/php/etc/php.ini</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加一行extension = redis.so</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">extension = redis.so</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E4%BD%BF%E7%94%A8%20redis%20%E5%82%A8%E5%AD%98/01.png?raw=true" alt="01"><figcaption class="figure__caption">01</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;/usr/local/php/bin/php -m |grep redis 检查是否成功</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@redis ~]<span class="comment"># /usr/local/php/bin/php -m |grep redis</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E4%BD%BF%E7%94%A8%20redis%20%E5%82%A8%E5%AD%98/02.png?raw=true" alt="02"><figcaption class="figure__caption">02</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <a href="https://hcldirgit.github.io/2017/08/24/Redis/6.%20PHP-redis%E4%B8%AD%E6%96%87%E6%96%87%E6%A1%A3/" target="_blank" rel="external">PHP-redis中文文档</a></p>
<h2 id="2-redis-实现-session-共享"><a href="#2-redis-实现-session-共享" class="headerlink" title="2. redis 实现 session 共享"></a>2. redis 实现 session 共享</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;php.ini中加入（不建议）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">session.save_handler = <span class="string">"redis"</span></div><div class="line">session.save_path = <span class="string">"tcp://127.0.0.1:6379"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者apache虚拟主机加入</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">php_value session.save_handler <span class="string">"redis"</span></div><div class="line">php_value session.save_path <span class="string">"tcp://127.0.0.1:6379"</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E4%BD%BF%E7%94%A8%20redis%20%E5%82%A8%E5%AD%98/03.png?raw=true" alt="03"><figcaption class="figure__caption">03</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者php-fpm.conf对应的pool中加入</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">php_value[session.save_handler] = redis</div><div class="line">php_value[session.save_path] = <span class="string">" tcp://127.0.0.1:6379 "</span></div></pre></td></tr></table></figure>
<h2 id="3-redis-储存-session-测试"><a href="#3-redis-储存-session-测试" class="headerlink" title="3. redis 储存 session 测试"></a>3. redis 储存 session 测试</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@redis ~]<span class="comment"># wget http://study.lishiming.net/.mem_se.txt</span></div><div class="line">[root@redis ~]<span class="comment"># mv .mem_se.txt /usr/local/apache2/htdocs/session.php</span></div><div class="line">[root@redis ~]<span class="comment"># curl localhost/session.php</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E4%BD%BF%E7%94%A8%20redis%20%E5%82%A8%E5%AD%98/04.png?raw=true" alt="04"><figcaption class="figure__caption">04</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输出类似于 1489247886<br><br>1489248876<br><br>v2k9k36f309jp5be74uussp3q4</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;连接redis：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@redis phpredis-develop]<span class="comment"># redis-cli </span></div><div class="line">127.0.0.1:6379&gt; keys *</div><div class="line">1) <span class="string">"PHPREDIS_SESSION:v2k9k36f309jp5be74uussp3q4"</span></div><div class="line">2) <span class="string">"PHPREDIS_SESSION:36htag7ctc7ueq60hreubhaho3"</span></div><div class="line">127.0.0.1:6379&gt; get PHPREDIS_SESSION:v2k9k36f309jp5be74uussp3q4</div><div class="line"><span class="string">"TEST|i:1489247886;TEST3|i:1489247886;"</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E4%BD%BF%E7%94%A8%20redis%20%E5%82%A8%E5%AD%98/05.png?raw=true" alt="05"><figcaption class="figure__caption">05</figcaption></figure></p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Redis/5. php 使用 redis 储存/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
